/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:LoginController.java
 * Date:2021/11/08 13:06:08
 */

package com.controller.UserController;

import com.config.myAnnotation.Log;
import com.config.myAnnotation.PassToken;
import com.pojo.Message;
import com.pojo.User;
import com.service.UserLoginService;
import com.service.redisService;
import com.utils.HttpContextUtils;
import com.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录实现
 */
@Api(tags="用户登录接口")
@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    UserLoginService loginService;
    @Autowired
    redisService server;

    /**
     * 登录
     * @param user 用户
     * @param response 返回一个cookie
     */
    @CrossOrigin
    @ApiOperation("登录接口")
    @ApiResponses({
            @ApiResponse(code=401,message="登陆失败"),
            @ApiResponse(code=200,message="注册成功")
    })
    @PostMapping("/login")
    @PassToken
    public ResponseEntity  login(@RequestBody User user, HttpServletResponse response){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String email=user.getEmail();
        String pwd=user.getPassword();
        String token = request.getHeader("token");

        Message msg = loginService.Login(email, pwd,token);
        //登陆失败
        if(msg.getFlag()!=200){
            return ResponseEntity.ok(msg);

        }else{
            //校验成功，加入cookie
            Cookie cookie = new Cookie("token",msg.getMsg());
            // 返回token
            response.addCookie(cookie);
        }
        return ResponseEntity.ok(msg);
    }
    @CrossOrigin
    @PostMapping("/test")
    public ResponseEntity test( ){
        System.out.println("访问到了/test");
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        System.out.println(  token);
        System.out.println(JwtUtils.getEmail(token));
        Message msg =new Message();
        msg.setFlag(200);
        msg.setMsg("访问到了test");
        return ResponseEntity.ok(msg);
    }
    @Log("执行test1")
    @GetMapping("/test1")
    public void test1(){
        System.out.println("访问到了/test,get");
    }

    /**
     * 退出登录，删除服务器的token
     * @return
     */
    @CrossOrigin
    @GetMapping("/logout")
    public String logOut(){
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String token = request.getHeader("token");
        server.del("sys:user:token" + token);
        return "已退出登录";
    }

    /**
     * 前端发过来判断token是否失败
     * @param request 请求头，拿来获取token
     * @return 返回是否有效
     */
    @ApiOperation("判断用户登录状态的接口，虚的，主要是通过拦截器返回信息")
    @CrossOrigin
    @GetMapping("/judgeRole")
    public boolean judgeRole(HttpServletRequest request){
       return true;
    }
}
